package com.example.administrator.school.fragment;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.school.R;
import com.example.administrator.school.db.MediaHelper;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.widget.MsgView;
import com.xiumiing.uilibrary.base.BaseFragment;

import java.util.ArrayList;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.xiumiing.uilibrary.Constans.TITLE;


public class FileFragment extends BaseFragment {

    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_member_size)
    TextView mTvMemberSize;
    @BindView(R.id.tv_video_count)
    TextView mTvVideoCount;
    @BindView(R.id.tv_alliance_revenue_title)
    TextView mTvAllianceRevenueTitle;
    @BindView(R.id.tv_document_count)
    TextView mTvDocumentCount;
    @BindView(R.id.tv_alliance_balance_title)
    TextView mTvAllianceBalanceTitle;
    @BindView(R.id.app_bar_layout)
    AppBarLayout mAppBarLayout;
    @BindView(R.id.TabLayout)
    SlidingTabLayout mTabLayout;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    Unbinder unbinder;
    private ArrayList<Fragment> mFragments;

    public static FileFragment newInstance(String title) {
        FileFragment fragment = new FileFragment();
        Bundle args = new Bundle();
        args.putString(TITLE, title);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_file, container, false);
        unbinder = ButterKnife.bind(this, view);
        mFileFragment = this;
        mFragments = new ArrayList<>();
        mFragments.add(FileListFragment.newInstance("视频", 0));
        mFragments.add(FileListFragment.newInstance("图片", 1));
        mFragments.add(FileListFragment.newInstance("文档", 2));
//        mFragments.add(FileListFragment.newInstance("语音", MEDIA_TYPE_AUDIO));
        mTabLayout.setViewPager(mViewPager, new String[]{"视频", "图片", "文档"}, Objects.requireNonNull(getActivity()), mFragments);

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        onRefresh();
    }

    public static FileFragment mFileFragment;

    public static FileFragment getInstance() {
        return mFileFragment;
    }

    public void onRefresh() {
        mTvMemberSize.setText("上传文件数量: " + MediaHelper.getInstance().getAllSize() + "（条）");
        mTvVideoCount.setText(MediaHelper.getInstance().getSize(0) + "");
        mTvDocumentCount.setText(MediaHelper.getInstance().getSize(2) + "");
        mTabLayout.setMsgMargin(1,10,0);
        MsgView msgView = mTabLayout.getMsgView(1);
        int size = MediaHelper.getInstance().getSize(1);
        msgView.setVisibility(size == 0 ? View.GONE : View.VISIBLE);
        msgView.setPadding(10,0,10,0);
        msgView.setText(size + "");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
